package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 获取套餐分页
     * 请求网址: http://localhost:8080/setmeal/page?page=1&pageSize=10
     * 请求方法: GET
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("prage = {},PageSize = {},name= {}",page,pageSize,name);

        //构造分页构造器
        Page<Setmeal> pageInfo = new Page(page,pageSize);
        Page<SetmealDto> dtoInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(name != null,Setmeal::getName,name);
        //添加排序条件
        //执行查询
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoInfo,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) ->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            // 根据分类id查询分类Name
            Category category = categoryService.getById(categoryId);
            if (category!=null)setmealDto.setCategoryName(category.getName());
            return setmealDto;


        }).collect(Collectors.toList());
        dtoInfo.setRecords(list);
        return R.success(dtoInfo);
    }
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功喽");
    }
    /**
     * 更新套餐
     * 求网址: http://localhost:8080/setmeal
     * 请求方法: PUT
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto:{}",setmealDto);
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功喽");
    }
    /**
     * 删除套餐
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        //log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除成功喽");
    }


    /**
     * 对setmeal进行停售
     * 请求网址: http://localhost:8080/setmeal/status/0?ids=1632906731342336002,1632741640378519554
     * 请求方法: POST
     */
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable int status,@RequestParam List<Long> ids){

        //log.info("status:{},ids:{}",status,ids);
        //update setmeal set status = 0 where id in (ids)
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealService.update(setmeal,queryWrapper);

        return R.success("批量停售啦");
    }


    /**
     * 修改套餐
     * 请求网址: http://localhost:8080/setmeal/1632910179823640578
     * 请求方法: GET
     */
    @GetMapping("/{id}")
    public R<SetmealDto> edit(@PathVariable Long id){
        //log.info("id:{}",id);
        SetmealDto setmealDto = setmealService.backData(id);
        return R.success(setmealDto);
    }

    /**
     * 根据条件查询套餐数据
     * @param
     * @return
     */

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);


        return R.success(list);
    }


}
